package com.oa.ylms.apps.actions.file;

import java.util.List;

import net.sf.json.JSONObject;

import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.file.IFileCategoryService;
import com.oa.ylms.apps.business.file.IFileInfoService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.entity.file.FileCategoryValue;
import com.oa.ylms.apps.entity.file.FileInfoValue;

@SuppressWarnings("serial")
public class FileInfoAction extends BasicAction {
	
	private List<FileInfoValue> infoList;
	private List<FileCategoryValue> categoryList;
	private IFileCategoryService categoryService;
	private IFileInfoService infoServicce;
	
	private Integer categoryId;
	private Integer fileId;
	private String fileIds;
	private String title;
	private String createTime;
	private Integer state;
	private String categoryName;
	private String fileUrl;
	private String searchTime;
	private String searchTitle;
	
	private String fileInfoJsonString;
	
	private String str;

	@Override
	public String execute() throws Exception {
		if("".equals(searchTime))
			createTime = null;
		else{
			createTime = searchTime;
		}
		if("".equals(searchTitle))
			title = null;
		else{
			title = searchTitle;
		}
		totalpage = infoServicce.countTPages(categoryId, title, createTime, state, rows);
		count = infoServicce.getTotalNumber();
		if(count%rows==0&&totalpage+1==currpage){
			currpage=totalpage;
		}
		categoryList = categoryService.queryCategoryByStates(IDEDStates.ENABLE);
		infoList = infoServicce.queryFileInfoWithPage(categoryId, title, createTime, state, rows,currpage);
		return "show";
	}
	
	public String toDisable() throws Exception{
		if(infoServicce.toDisable(fileId)){
			return "toDisable";
		}
		return "false";
	}
	
	public String toEnable() throws Exception{
		if(infoServicce.toEnable(fileId)){
			return "toEnable";
		}
		return "false";
	}
	
	public String add() throws Exception{
		categoryId = null;
		title = null;
		try {
			JSONObject json = JSONObject.fromObject(fileInfoJsonString);
			if(!json.get("categoryId").equals("")){
				categoryId = json.getInt("categoryId");
			}
			if(!json.get("title").equals("")){
				title= json.getString("title");
			}
			fileUrl = "file/"+title;
			if(infoServicce.addFileInfo(title, fileUrl, categoryId)>0){
				str = "1";
			}else{
				str = "-1";
			}
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().write(str);
		} catch (Exception e) {
			return "false";
		}
		return null;
	}
	
	public String addCategory() throws Exception{
		categoryName = null;
		try {
			JSONObject json = JSONObject.fromObject(fileInfoJsonString);
			if(!json.get("categoryName").equals("")){
				categoryName = json.getString("categoryName");
			}
			categoryService.addFileCategory(categoryName);
		} catch (Exception e) {
			return "false";
		}
		return null;
	}
	
	public String delete() throws Exception{
		if(infoServicce.deleteFileInfo(fileIds)){
			return "delete";
		}
		return "false";
	}
	
	
	
	
	//
	
	

	public List<FileInfoValue> getInfoList() {
		return infoList;
	}

	public String getFileUrl() {
		return fileUrl;
	}

	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public String getFileInfoJsonString() {
		return fileInfoJsonString;
	}

	public void setFileInfoJsonString(String fileInfoJsonString) {
		this.fileInfoJsonString = fileInfoJsonString;
	}

	public void setInfoList(List<FileInfoValue> infoList) {
		this.infoList = infoList;
	}

	public List<FileCategoryValue> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<FileCategoryValue> categoryList) {
		this.categoryList = categoryList;
	}

	public IFileCategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(IFileCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public IFileInfoService getInfoServicce() {
		return infoServicce;
	}

	public void setInfoServicce(IFileInfoService infoServicce) {
		this.infoServicce = infoServicce;
	}

	public void setFileId(Integer fileId) {
		this.fileId = fileId;
	}

	public Integer getFileId() {
		return fileId;
	}

	public void setFileIds(String fileIds) {
		this.fileIds = fileIds;
	}

	public String getFileIds() {
		return fileIds;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setSearchTime(String searchTime) {
		this.searchTime = searchTime;
	}

	public String getSearchTime() {
		return searchTime;
	}

	public void setSearchTitle(String searchTitle) {
		this.searchTitle = searchTitle;
	}

	public String getSearchTitle() {
		return searchTitle;
	}

	public void setStr(String str) {
		this.str = str;
	}

	public String getStr() {
		return str;
	}

}
